package pruebascroll;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.event.AdjustmentEvent;
import java.beans.PropertyChangeEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.AdjustmentListener;
import java.beans.PropertyChangeListener;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author tekoraik
 */
public class VistaPrincipal extends JFrame{
    JTable jt;
    JScrollPane jsp;
    SpringLayout layout;
    DefaultTableModel dtm;
    BoundedRangeModel brm;
    Controlador c;
    int kant=0,iant=0;
    public VistaPrincipal(Controlador c){
        this.c=c;
        jt=new JTable();
        jsp=new JScrollPane(jt);
        layout = new SpringLayout();
        dtm = new DefaultTableModel();
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);              
        dtm.addColumn("Palabra");
        dtm.setRowCount(c.getNumRows());
        jt.setModel(dtm);

        this.setSize(400,300);
        this.setTitle("Prueba Scroll");
        this.getContentPane().add(jsp);
        
        layout.putConstraint(SpringLayout.NORTH, jsp, 10, SpringLayout.NORTH, this.getContentPane());
        layout.putConstraint(SpringLayout.SOUTH, jsp, -10, SpringLayout.SOUTH, this.getContentPane());
        layout.putConstraint(SpringLayout.EAST, jsp, -10, SpringLayout.EAST, this.getContentPane());
        layout.putConstraint(SpringLayout.WEST, jsp, 10, SpringLayout.WEST, this.getContentPane());
        this.getContentPane().setLayout(layout);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jsp.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener(){

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                refrescarTabla();
            }
        });
        
        
    }
    public void borrarIK(int i, int k){
        while (i<=(i+k) && i<dtm.getRowCount()){
            dtm.setValueAt("", i, 0);
            i++;
        }
    }
    public void rellenarIK(int i, int k,String[] s){
        int j=0;
        int ii=i;
        while (ii<(i+k) && ii<dtm.getRowCount()){
            dtm.setValueAt(s[j], ii, 0);
            ii++;
            j++;
        }
    }
    public void refrescarTabla(){
        int i,k;
        if (kant==0 && iant ==0){
            i=1;
            k=cuantasFilas();
        } else {
            borrarIK(iant,kant);
            i=queFila()+1;
            k=cuantasFilas();

        }
        String[] s = c.getRows(i, k);
        rellenarIK(i,k,s);
        kant=k;
        iant=i;
    }
    public void rellenaTabla(){
        dtm = new DefaultTableModel();
        jt.setModel(dtm);
        int k = cuantasFilas();
        
        
    }
    public int cuantasFilas(){
        return jsp.getViewport().getHeight()/jt.getRowHeight()-1;
    }
    public int queFila(){
       return jsp.getVerticalScrollBar().getValue()/jt.getRowHeight();
    }
    
}
